//
//  YWKButtonImgTop.m
//  WDDoctor
//
//  Created by  WangDan on 16/3/18.
//  Copyright © 2016年 iCareTech. All rights reserved.
//


#import "TCButtonImgTop.h"
#import "TCConfiguration.h"

@interface TCButtonImgTop ()

@property (nonatomic) CGSize titleSize;

@end

@implementation TCButtonImgTop

#pragma mark - life cycle

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.imageView.contentMode = UIViewContentModeCenter;   //头像不随视图拉伸

        self.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 3;
        
        //设置默认字体大小
        _firstFontSize = kFontSize;
        _secondFontSize = kFontSize;
        _isRequiredShowSecondLine = NO;
    }
    
    return self;
}

- (instancetype)initRoundImageWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
//        self.imageView.contentMode = UIViewContentModeCenter;   //头像不随视图拉伸
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView.layer.cornerRadius = 25;
        self.imageView.clipsToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 3;
        
        //设置默认字体大小
        _firstFontSize = kFontSize;
        _secondFontSize = kFontSize;
        _isRequiredShowSecondLine = NO;
    }
    
    return self;
}

#pragma mark - private methods
- (NSMutableAttributedString *)getAttibuteTitle {
    NSAttributedString *attFirstLine = [[NSAttributedString alloc] initWithString:[self.firstLineString
                                                                                   substringWithRange:NSMakeRange(0, self.firstLineString.length)]
                                                                       attributes:@{NSFontAttributeName:[UIFont fontWithName:kTextFontName size:self.firstFontSize],
                                                                                    NSForegroundColorAttributeName:self.firstLineColor}];
    
    NSAttributedString *attReturn = [[NSAttributedString alloc] initWithString:@"\n"
                                                                    attributes:@{NSFontAttributeName:[UIFont fontWithName:kTextFontName size:self.firstFontSize]}];
    
    //主标题与副标题间距
    NSAttributedString *attSecondOffset = [[NSAttributedString alloc] initWithString:@"\t\n"
                                                                    attributes:@{NSFontAttributeName:[UIFont fontWithName:kTextFontName size:self.secondFontSize]}];
    
    if ([self.secondLineString isEqualToString:@""]) {
        
        if (self.isRequiredShowSecondLine) {
            
            self.secondLineString = @"\t";
        }
    }
    NSAttributedString *attSecondLine = [[NSAttributedString alloc] initWithString:[self.secondLineString
                                                                                    substringWithRange:NSMakeRange(0, self.secondLineString.length)]
                                                                        attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.secondFontSize],
                                                                                     NSForegroundColorAttributeName:self.secondLineColor}];
    
    NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc] initWithAttributedString:attFirstLine];
    if (self.secondLineString && ![self.secondLineString isEqualToString:@""]) {
        
        [attTitle appendAttributedString:attReturn];
        [attTitle appendAttributedString:attSecondOffset];
        [attTitle appendAttributedString:attSecondLine];
    }
    return attTitle;
}

#pragma mark - override methods

- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    
    CGFloat contentHeight = self.imgSize.height + self.titleSize.height + self.firstLineTopOffset;
    
    CGFloat titleX = 0;
    CGFloat titleY = 0.5 * (contentRect.size.height - contentHeight) + self.imgSize.height + self.firstLineTopOffset;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = self.titleSize.height;

    return CGRectMake(titleX, titleY, titleW, titleH);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    
    CGFloat contentHeight = self.imgSize.height + self.titleSize.height + self.firstLineTopOffset;
    
    CGFloat imgX = 0.5 * (contentRect.size.width - self.imgSize.width);
    CGFloat imgY = 0.5 * (contentRect.size.height - contentHeight);
    CGFloat imgW = self.imgSize.width;
    CGFloat imgH = self.imgSize.height;

    return CGRectMake(imgX, imgY, imgW, imgH);
}

#pragma mark - public methods

- (void)setAdjustTitleforState:(UIControlState)state{
    
    NSMutableAttributedString *attTitle = [self getAttibuteTitle];
    [self setAttributedTitle:attTitle forState:UIControlStateNormal];
}

- (CGRect)adjustFrame{
    
    CGRect rect = self.frame;
    
    CGFloat buttonW = self.imgSize.width > self.titleSize.width
                    ? self.imgSize.width + 2
                    : self.titleSize.width + 2;
    
    CGFloat buttonH = self.imgSize.height
                    + self.titleSize.height
                    + self.firstLineTopOffset;

    return CGRectMake(rect.origin.x, rect.origin.y, buttonW, buttonH);
}

#pragma mark - getters

- (NSString *)firstLineString{
    
    if (!_firstLineString) {
        
        _firstLineString = @"";
        
    }
    
    return _firstLineString;
}

- (NSString *)secondLineString{
    
    if (!_secondLineString) {
        
        _secondLineString = @"";
        
    }
    
    return _secondLineString;
}

- (UIColor *)firstLineColor{
    
    if (_firstLineColor == nil) {
        
        _firstLineColor = [UIColor blackColor];
        
    }
    return _firstLineColor;
}

- (UIColor *)secondLineColor{
    
    if (_secondLineColor == nil) {
        
        _secondLineColor = [UIColor lightGrayColor];
        
    }
    return _secondLineColor;
}

- (CGSize)titleSize{
    
    NSAttributedString *attTitle = [[self getAttibuteTitle] copy];
    _titleSize = [attTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                        context:nil].size;
    
    return _titleSize;
}

@end
